var fs=require('fs');
var Config=require('./Config');

var _404_html="./404/404_1.html";

function readFile(pathname,writeHead,response,callback){
	var temp_pathname=pathname.split('?');
	fs.readFile(temp_pathname[0], "binary", function(err, file) {
        if (err) {
        	fs.exists(temp_pathname[0],function(exists){
        		if(exists){
        			 response.writeHead(500, {'Content-Type': 'text/plain'});
    	             response.write('Server is error!');
    	             response.end();
        		}else{
        			readFile(_404_html,Config.type['html'],response);
        		}
        	});
           
        } else {
        	if(typeof callback==='function'){
        		callback(response,file);
        	}else{
        		response.writeHead(200, {'Content-Type': writeHead});
                response.write(file, "binary");
                response.end();
        	}
        }
     });
}

exports.readFile=readFile;